home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
TPUG - Toronto PET Users Group
/
TPUG Users Group CD
/
TPUG Users Group CD.iso
/
AMIGA
/
AMICUS
/
AMICUS04.ADF
/
C
/
timedely.c
< prev
next >
Wrap
C/C++ Source or Header
|
1985-10-30
|
5KB
|
145 lines
/* time delay program v1.0 */
/****************************************************************
* *
* Copyright 1985, Commodore Amiga Inc. All rights reserved. *
* No part of this program may be reproduced, transmitted, *
* transcribed, stored in retrieval system, or translated into *
* any language or computer language, in any form or by any *
* means, electronic, mechanical, magnetic, optical, chemical, *
* manual or otherwise, without the prior written permission of *
* Commodore Amiga Incorporated, 983 University Ave, #D *
* Los Gatos, CA 95030 *
* *
****************************************************************/
/* SIMPLE TIMER EXAMPLE PROGRAM:
*
* Includes dynamic allocation of data structures needed to communicate
* with the timer device as well as the actual device IO
*/
#include <exec/types.h>
#include <exec/lists.h>
#include <exec/nodes.h>
#include <exec/ports.h>
#include <exec/io.h>
#include <exec/devices.h>
#include <devices/timer.h>
#define SECONDS io_Actual
#define MICROSECONDS io_Length
/* redefine fields in IOStdReq so as to match requirements of a timeval */
struct Port *timerport;
struct IOStdReq *timermsg;
main()
{
printf("\ntimer test");
timedelay(2,0);
printf("\nAfter 2 seconds delay");
timedelay(4,0);
printf("\nAfter 4 seconds delay");
timedelay(0,500000); /* 500,000 seconds = 1/2 second */
printf("\nAfter 1/2 second delay");
printf("\nExiting now");
}
/* *********************************************************************** */
/* Timer function - timedelay(seconds,microseconds)
Your task is put to sleep for the specified time interval.
If seconds > 0, then UNIT_VBLANK is used, delay is in multiples of
60ths of a second. If seconds < 0, then UNIT_MICROHZ is used for
more precision.
Returns value of 0 if no errors, nonzero (and no task sleeping)
if there were errors. -1000 means error during CreatePort,
-2000 means error during CreateStdIO,
other errors returned by OpenDevice.
Notice that since this is a multi-tasking system, the delays
shown here must be considered to be only approximate.
Also note that this function is used primarily to show how
a timer device is accessed, including the creation of the
message port and a message structure (IOStdReq). Note that
there is a Delay(interval) function already in the DOS.library.
(See the DOS developer's manual for details).
The exec_support function CreateStdIO (in amiga.lib) is used
for convenience here, with two of the data structure's fields
redefined to match the information requirements of the timer
in a timeval structure.
*/
/* *********************************************************************** */
extern struct Port *CreatePort();
extern struct IOStdReq *CreateStdIO();
int
timedelay(seconds,microseconds)
ULONG seconds,microseconds;
{
SHORT error;
error = preparetimer();
if(error != 0) return(error);
set_timer(seconds,microseconds);
deletetimer();
return(0);
} /* end of timedelay */
int
preparetimer()
{
SHORT error;
SHORT whichunit;
timerport = CreatePort(0,0);
if (timerport == NULL)
return(-1000); /* Error during CreatePort */
timermsg = CreateStdIO(timerport);
if (timermsg == NULL)
{
DeletePort(timerport);
return(-2000); /* Error during CreateStdIO */
}
if(seconds > 0)
whichunit = UNIT_VBLANK;
else
whichunit = UNIT_MICROHZ;
error = OpenDevice(TIMERNAME, whichunit, timermsg, 0);
if (error != 0)
{
DeleteStdIO(timermsg);
DeletePort(timerport);
return(error); /* Error during OpenDevice */
}
return(0);
}
int
set_timer(seconds,microseconds)
ULONG seconds,microseconds;
{
timermsg->io_Command = TR_ADDREQUEST; /* add a new timer request */
timermsg->SECONDS = seconds; /* seconds */
timermsg->MICROSECONDS = microseconds; /* microseconds */
DoIO( timermsg ); /* post request to the timer */
/* goes to sleep till done */
return(0);
}
int
deletetimer()
{
DeleteStdIO(timermsg);
DeletePort(timerport);
return(0); /* No errors if 0 returned */
}